package com.yanqu.road.server.manger;

import com.yanqu.road.entity.activity.shuihuyezipai.*;
import com.yanqu.road.entity.activity.shuihuyezipai.config.ShuiHuYeZiCardActivityConfig;
import com.yanqu.road.entity.activity.shuihuyezipai.config.ShuiHuYeZiCardLevelConfig;
import com.yanqu.road.entity.log.LogShuiHuYeZiCardUserCard;
import com.yanqu.road.pb.activity.ShuiHuYeZiCardProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 水浒叶子牌通用类
 * @Author cwq
 * @Data 2021/12/2 17:52
 */
public class ShuiHuYeZiCardCommonMgr {


    public static Map<Integer,ShuiHuYeZiCardBattleCard> createBattleCard(List<Integer> cardList, Map<Integer, ShuiHuYeZiCardUserCard> userCardMap){
        Map<Integer,ShuiHuYeZiCardBattleCard> cardMap = new HashMap<>();
        for (Integer id: cardList){
            ShuiHuYeZiCardBattleCard battleCard = new ShuiHuYeZiCardBattleCard();
            battleCard.setId(id);
            ShuiHuYeZiCardUserCard cardUserCard = userCardMap.get(id);
            if(cardUserCard != null){
                battleCard.setLevel(cardUserCard.getLevel());
                battleCard.setQuality(cardUserCard.getQuality());
            }
            cardMap.put(battleCard.getId(),battleCard);
        }
        return cardMap;
    }

    /**
     * @param roomInfo 房间信息
     * @param userCardMap 对方的手牌
     * @param roundEndTime 回合剩余时间
     * @param userMap 玩家基本信息
     * @return
     */
    public static ShuiHuYeZiCardProto.ShuiHuYeZiCardRoomInfo.Builder builderShuiHuYeZiCardRoomInfo(AbstractShuiHuYeZiCardRoom roomInfo,Map<Long, Map<Integer, ShuiHuYeZiCardBattleCard>> userCardMap, long roundEndTime,Map<Long, PlayerProto.PlayerBaseTempMsg> userMap,int roomId){
        ShuiHuYeZiCardProto.ShuiHuYeZiCardRoomInfo.Builder builder = ShuiHuYeZiCardProto.ShuiHuYeZiCardRoomInfo.newBuilder();
        for (Map.Entry<Long,Map<Integer, ShuiHuYeZiCardBattleCard>> entry : userCardMap.entrySet()){
            // 玩家手牌
            ShuiHuYeZiCardProto.ShuiHuYeZiCardUserBattleInfo.Builder battleInfo = ShuiHuYeZiCardProto.ShuiHuYeZiCardUserBattleInfo.newBuilder();
            battleInfo.setUserId(entry.getKey());
            PlayerProto.PlayerBaseTempMsg playerBaseTempMsg = userMap.get(entry.getKey());
            if(playerBaseTempMsg != null){
                battleInfo.setPlayerInfo(playerBaseTempMsg);
            }
            for (ShuiHuYeZiCardBattleCard item : entry.getValue().values()){
                battleInfo.addCardList(builderShuiHuYeZiCardCardInfo(item));
            }
            builder.addBattleInfoList(battleInfo);
        }
        builder.setNpcId(roomInfo.getRoomInfo().getNpcId());
        builder.setBuffId(roomInfo.getRoomInfo().getBuffId());
        builder.setNum(roomInfo.getRoomInfo().getNum());
        builder.setNextBuffId(roomInfo.getRoomInfo().getNextBuffId());
        builder.setRoundEndTime(roundEndTime);
        builder.setRound(roomInfo.getRoomInfo().getRound());
        // 回合记录
        for (ShuiHuYeZiCardPlayRecord item : roomInfo.getRoomInfo().getRecordList()){
            ShuiHuYeZiCardProto.ShuiHuYeZiCardRecord.Builder cardBuilder = ShuiHuYeZiCardProto.ShuiHuYeZiCardRecord.newBuilder();
            for (Map.Entry<Long,ShuiHuYeZiCardBattleCard> entry : item.getRecordMap().entrySet()){
                cardBuilder.addUserRecordList(builderShuiHuYeZiCardRoundUserRecord(entry.getKey(),entry.getValue()));
            }
            cardBuilder.setWinnerId(item.getWinnerId());
            cardBuilder.setBuffId(item.getBuffId());
            builder.addCardRecordList(cardBuilder);
        }
        // 弃牌堆
        for (ShuiHuYeZiCardDiscardCard item : roomInfo.getRoomInfo().getDiscardList()){
            ShuiHuYeZiCardProto.ShuiHuYeZiCardCardInfo.Builder cardBuilder = ShuiHuYeZiCardProto.ShuiHuYeZiCardCardInfo.newBuilder();
            cardBuilder.setId(item.getId());
            cardBuilder.setLevel(item.getLevel());
            cardBuilder.setQuality(item.getQuality());
            builder.addDiscardCardList(cardBuilder);
        }
        builder.setRoomId(roomId);
        builder.setStartTime(roomInfo.getRoomInfo().getStartTime());
        return builder;
    }

    public static ShuiHuYeZiCardProto.ShuiHuYeZiCardCardInfo.Builder builderShuiHuYeZiCardCardInfo(ShuiHuYeZiCardBattleCard item){
        ShuiHuYeZiCardProto.ShuiHuYeZiCardCardInfo.Builder builder = ShuiHuYeZiCardProto.ShuiHuYeZiCardCardInfo.newBuilder();
        builder.setId(item.getId());
        builder.setLevel(item.getLevel());
        builder.setQuality(item.getQuality());
        builder.setValue(item.getValue());
        return builder;
    }

    public static ShuiHuYeZiCardProto.ShuiHuYeZiCardRoundUserRecord.Builder builderShuiHuYeZiCardRoundUserRecord(long userId,ShuiHuYeZiCardBattleCard shuiHuYeZiCardBattleCard){
        ShuiHuYeZiCardProto.ShuiHuYeZiCardRoundUserRecord.Builder roundRecordBuilder = ShuiHuYeZiCardProto.ShuiHuYeZiCardRoundUserRecord.newBuilder();
        roundRecordBuilder.setUserId(userId);

        roundRecordBuilder.setId(shuiHuYeZiCardBattleCard.getId());
        roundRecordBuilder.setLevel(shuiHuYeZiCardBattleCard.getLevel());
        roundRecordBuilder.setQuality(shuiHuYeZiCardBattleCard.getQuality());
        roundRecordBuilder.setValue(shuiHuYeZiCardBattleCard.getValue());
        return roundRecordBuilder;
    }


    /**
     * 是否在匹配区间
     * @return
     */
    public static boolean inMatchTime(ShuiHuYeZiCardActivityConfig activityConfig){
        int todaySecond = DateHelper.getTodaySecond();
        int minute = (int)(todaySecond / DateHelper.MINUTE_SECONDS);
        for(Integer[] item : activityConfig.openTimeList){
            if(minute >= item[0] && minute < item[1]){
                return true;
            }
        }

        return false;
    }

    /**
     * 是否有可能有pvp对战
     * @return
     */
    public static boolean inPvpTime(ShuiHuYeZiCardActivityConfig activityConfig){
        int todaySecond = DateHelper.getTodaySecond();
        int minute = (int)(todaySecond / DateHelper.MINUTE_SECONDS);
        for(Integer[] item : activityConfig.openTimeList){
            if(minute >= item[0] && minute < item[1] + 5){
                return true;
            }
        }

        return false;
    }



    /**
     * 当前积分对应段位
     * @param activityConfig
     * @param score
     * @return
     */
    public static int getLevel(ShuiHuYeZiCardActivityConfig activityConfig,int score){
        int level = 0;
        for (ShuiHuYeZiCardLevelConfig item : activityConfig.getLevelConfigList()){
            level = item.getId();
            if(item.getScore() <= 0 || item.getScore() > score) {
                break;
            }
        }
        return level;
    }


    public static List<LogShuiHuYeZiCardUserCard> createLogShuiHuYeZiCardUserCardList(Map<Integer, ShuiHuYeZiCardBattleCard> map){
        List<LogShuiHuYeZiCardUserCard> list = new ArrayList();
        for (ShuiHuYeZiCardBattleCard item : map.values()){
            LogShuiHuYeZiCardUserCard logShuiHuYeZiCardUserCard = new LogShuiHuYeZiCardUserCard();
            logShuiHuYeZiCardUserCard.setId(item.getId());
            logShuiHuYeZiCardUserCard.setLv(item.getLevel());
            logShuiHuYeZiCardUserCard.setQ(item.getQuality());
            logShuiHuYeZiCardUserCard.setV(item.getValue());
            list.add(logShuiHuYeZiCardUserCard);
        }
        return list;
    }


}
